package com.zbkj.admin.service.impl;

import com.github.pagehelper.PageInfo;
import com.zbkj.admin.service.AdminPartnerHandler;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.model.user.Partner;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.user.AdminPartnerSaveRequest;
import com.zbkj.common.utils.RandomCodeUtil;
import com.zbkj.service.service.user.PartnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Auther:
 * @Date: 2024/9/11 22:21
 * @Description:
 */
@Service
public class AdminPartnerHandlerImpl implements AdminPartnerHandler {

    @Autowired
    private PartnerService partnerService;

    @Override
    public String getInviteCode() {
        int retryNum = 5;
        String inviteCode = null;
        while (retryNum > 0) {
            inviteCode = RandomCodeUtil.getRandomCode(8);
            int existNum = partnerService.getCountByIviteCode(inviteCode);
            if(existNum > 0) {
                retryNum -= 1;
            } else {
                break;
            }
        }
        if(StringUtils.isBlank(inviteCode)) {
            throw new CommonException("邀请码生成失败，请重试");
        }
        return inviteCode;
    }

    @Override
    public PageInfo<Partner> list(PageParamRequest queryRequest) {
        return partnerService.adminGetlist(queryRequest);
    }

    @Override
    public void saveOrUpdate(AdminPartnerSaveRequest partnerRequest) {
        int phoneNum = partnerService.getCountByPhone(partnerRequest.getPhone(), partnerRequest.getId());
        if(phoneNum > 0) {
            throw new CommonException("手机号已存在，不能重复添加");
        }
        if(Objects.isNull(partnerRequest.getId())) {
            int existNum = partnerService.getCountByIviteCode(partnerRequest.getInviteCode());
            if(existNum > 0) {
                throw new CommonException("邀请码已被占用，请重试");
            }
            Partner partner = new Partner();
            BeanUtils.copyProperties(partnerRequest, partner);
            partnerService.save(partner);
        } else {
            Partner partner = new Partner();
            BeanUtils.copyProperties(partnerRequest, partner);
            partner.setInviteCode(null);
            partnerService.updateById(partner);
        }
    }

    @Override
    public Partner getInfo(Long id) {
        return partnerService.getPartnerById(id);
    }

    @Override
    public void delPartner(Long id) {
        Partner dbPartner = partnerService.getPartnerById(id);
        if(Objects.isNull(dbPartner)) {
            throw new CommonException("合作伙伴信息已不存在");
        }
        Partner upPartner = new Partner();
        upPartner.setId(id);
        upPartner.setDeleteFlag(1);
        upPartner.setPhone(dbPartner.getPhone()+"_d"+dbPartner.getId());
        partnerService.updateById(upPartner);
    }
}
